﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using HelpCentral.Data.Model;
using HelpCentral.Shared.Interfaces;
using HelpCentral.Shared.Enums;
using HelpCentral.Configuration;

namespace HelpCentral.Data.Repository
{
    internal static class AuditEventExtensions
    {
        public static IAuditEvent FromRepositoryObject(this AuditEvent source)
        {
            if (source == null) return null;

            var ae = CoreContainer.ResolveNew<IAuditEvent>();

            ae.AuditId = source.AuditId;
            ae.AffectedObjectId = source.AffectedObjectId;
            ae.SecondAffectedObjectId = source.SecondAffectedObjectId;
            ae.Client = (source.Client == null) ? null : source.Client.FromRepositoryObject();
            ae.Date = source.Date;
            ae.EventType = (AuditEventType)Enum.Parse(typeof(AuditEventType), source.EventType, true);
            ae.Information = source.Information;

            return ae;
        }

        public static AuditEvent ToRepositoryObject(this IAuditEvent source, HelpCentralModelContainer context)
        {
            if (source == null) return null;

            var ae = context.AuditEvents.FirstOrDefault(x => x.AuditId == source.AuditId);

            if (ae == null)
            {
                ae = new AuditEvent();
            }

            ae.AffectedObjectId = source.AffectedObjectId;
            ae.SecondAffectedObjectId = source.SecondAffectedObjectId;
            ae.Client = (source.Client == null) ? null : source.Client.ToRepositoryObject(context);
            ae.Date = source.Date;
            ae.EventType = source.EventType.ToString();
            ae.Information = source.Information;

            return ae;
        }
    }
}
